package com.school.controller.admin;


import com.school.common.TeaShopCategoryLevelEnum;
import com.school.entity.GoodsCategory;
import com.school.service.GoodsCategoryService;

import com.school.util.PageQueryUtil;
import com.school.util.PageResult;
import com.school.util.Result;
import com.school.util.ResultGenerator;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class GoodsCategoryController {

    @Resource
    private GoodsCategoryService goodsCategoryService;

    @GetMapping("/categories")
    public String categories(HttpServletRequest request,
                             @RequestParam("categoryLevel") Byte categoryLevel,
                             @RequestParam("parentId") Long parentId,
                             @RequestParam("backParentId") Long backParentId) {
        if (categoryLevel == null || parentId == null || backParentId == null) {
            return "admin/error/error_400";
        }
        request.setAttribute("path", "tea_shop_category");
        request.setAttribute("parentId", parentId);
        request.setAttribute("backParentId", backParentId);
        request.setAttribute("categoryLevel", categoryLevel);
        return "admin/tea_shop_category";
    }

    @GetMapping("/categories/list")
    @ResponseBody
    public Result list(@RequestParam Map<String, Object> params) {
        if (StringUtils.isEmpty(params.get("page")) ||
                StringUtils.isEmpty(params.get("limit")) ||
                params.get("categoryLevel") == null ||
                params.get("parentId") == null) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        PageQueryUtil pageQueryUtil = new PageQueryUtil(params);
        PageResult pageResult = goodsCategoryService.getCategorysPage(pageQueryUtil);
        return ResultGenerator.genSuccessResult(pageResult);
    }

@GetMapping("/categories/listForSelect")
@ResponseBody
public Result listForSelect(@RequestParam("categoryId") Long categoryId) {
    if (categoryId == null || categoryId < 1) {
        return ResultGenerator.genFailResult("参数异常！");
    }
    GoodsCategory goodsCategory = goodsCategoryService.getGoodsCategoryById(categoryId);
    if (goodsCategory == null || goodsCategory.getCategoryLevel() == TeaShopCategoryLevelEnum.LEVEL_THREE.getLevel()) {
        return ResultGenerator.genFailResult("参数异常！");
    }
    Map categoryResult = new HashMap();
    if (goodsCategory.getCategoryLevel() == TeaShopCategoryLevelEnum.LEVEL_ONE.getLevel()) {
        // 返回当前一级分类目录下所有的二级分类，以及二级分类下的所有三级分类
        List<GoodsCategory> secondLevelCategories = goodsCategoryService.selectByLevelAndParentIdAndNumber(Collections.singletonList(categoryId), TeaShopCategoryLevelEnum.LEVEL_TWO.getLevel());
        if (!secondLevelCategories.isEmpty()) {
            Long categoryID1 = secondLevelCategories.get(0).getCategoryId();
            List<GoodsCategory> thirdLevelCategories = goodsCategoryService.selectByLevelAndParentIdAndNumber(Collections.singletonList(categoryID1), TeaShopCategoryLevelEnum.LEVEL_THREE.getLevel());
            categoryResult.put("secondLevelCategories", secondLevelCategories);
            categoryResult.put("thirdLevelCategories", thirdLevelCategories);
        }
    }
    if (goodsCategory.getCategoryLevel() == TeaShopCategoryLevelEnum.LEVEL_TWO.getLevel()) {
        // 返回当前二级分类目录下所有的三级分类
        List<GoodsCategory> thirdLevelCategories = goodsCategoryService.selectByLevelAndParentIdAndNumber(Collections.singletonList(categoryId), TeaShopCategoryLevelEnum.LEVEL_THREE.getLevel());
        categoryResult.put("thirdLevelCategories", thirdLevelCategories);
    }
    return ResultGenerator.genSuccessResult(categoryResult);
}

}